﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace AgvDispatchSystem
{
    public partial class FrmAgvManager : Form
    {
        public FrmAgvManager()
        {
            InitializeComponent();
        }

        private void FrmAgvManager_Load(object sender, EventArgs e)
        {
            XDocument xdoc = XDocument.Load(@"..\..\data\agvInfo.xml");
            XElement rootEle = xdoc.Root;
            TreeNode rootnode = tvAgv.Nodes.Add(rootEle.Name.ToString());
            LoadXmlTreeView(rootEle, rootnode.Nodes);

        }

        private void LoadXmlTreeView(XElement rootEle, TreeNodeCollection nodes)
        {

            //获取根元素的所有子元素
            rootEle.Elements();
            //遍历根元素所有的子元素
            foreach (XElement item in rootEle.Elements())
            {
                if (item.Elements().Count() == 0)
                {
                    nodes.Add(item.Name.ToString()).Nodes.Add(item.Value.ToString());
                }
                else
                {
                    //当前子元素加载到TreeView的节点集合中
                    TreeNode node = nodes.Add(item.Name.ToString());
                    LoadXmlTreeView(item, node.Nodes);
                }
            }
        }
    }
}
